<?php

  class MailAttachment extends Base {

    /** @var string The name of the file to attach. */
    private $filename;

    /** @var string The content of the file of the file to attach. */
    private $filecontent;

    /** @var string The MIME type of the file to attach. */
    private $mimetype;

    /** @var string A random hash for mail headers. */
    private $hash;

    /** @var array Most popumlar MIME types. */
    private $types = array("txt" => "text/plain",
        "htm" => "text/html",
        "html" => "text/html",
        "css" => "text/css",
        "png" => "image/png",
        "gif" => "image/gif",
        "jpg" => "image/jpeg",
        "jpeg" => "image/jpeg",
        "bmp" => "image/bmp",
        "tif" => "image/tiff",
        "bz2" => "application/x-bzip",
        "gz" => "application/x-gzip",
        "tar" => "application/x-tar",
        "zip" => "application/zip",
        "aif" => "audio/aiff",
        "aiff" => "audio/aiff",
        "mid" => "audio/mid",
        "midi" => "audio/mid",
        "mp3" => "audio/mpeg",
        "ogg" => "audio/ogg",
        "wav" => "audio/wav",
        "wma" => "audio/x-ms-wma",
        "asf" => "video/x-ms-asf",
        "asx" => "video/x-ms-asf",
        "avi" => "video/avi",
        "mpg" => "video/mpeg",
        "mpeg" => "video/mpeg",
        "wmv" => "video/x-ms-wmv",
        "wmx" => "video/x-ms-wmx",
        "xml" => "text/xml",
        "xsl" => "text/xsl",
        "doc" => "application/msword",
        "rtf" => "application/msword",
        "xls" => "application/excel",
        "pps" => "application/vnd.ms-powerpoint",
        "ppt" => "application/vnd.ms-powerpoint",
        "pdf" => "application/pdf",
        "ai" => "application/postscript",
        "eps" => "application/postscript",
        "psd" => "image/psd",
        "swf" => "application/x-shockwave-flash",
        "ra" => "audio/vnd.rn-realaudio",
        "ram" => "audio/x-pn-realaudio",
        "rm" => "application/vnd.rn-realmedia",
        "rv" => "video/vnd.rn-realvideo",
        "exe" => "application/x-msdownload",
        "pls" => "audio/scpls",
        "m3u" => "audio/x-mpegurl");

    /**
     * Prepare all informations for mail headers to attach the file.
     * @param string $filename The name of the file to attach.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.file-exists.php
     * @link http://be2.php.net/manual/en/function.chunk-split.php
     * @link http://be2.php.net/manual/en/function.base64-encode.php
     * @link http://be2.php.net/manual/en/function.file-get-contents.php
     * @link http://be2.php.net/manual/en/function.md5.php
     * @link http://be2.php.net/manual/en/function.time.php
     * @link http://be2.php.net/manual/en/function.substr.php
     * @link http://be2.php.net/manual/en/function.strrpos.php
     */
    public function __construct(IString $filename = null) {
      if ($filename != null && file_exists($filename->toString())) {
        $this->filename = $filename->toString();
        $this->filecontent = chunk_split(base64_encode(file_get_contents($filename->toString())));
        $this->hash = md5(time());
        $this->mimetype = $this->types[substr($filename->toString(), strrpos($filename->toString(), ".") + 1)];
      } elseif ($filename != null) {
        throw new FileNotFoundException("The file '" . $filename->toString() . "' was not found.");
      }
    }

    /**
     * Get the name of the file to attach.
     * @return String The name of the file to attach.
     */
    public function getFilename() {
      return new String(basename($this->filename));
    }

    /**
     * Get the content of the file to attach.
     * @return String The content of the file to attach.
     */
    public function getFileContent() {
      return new String($this->filecontent);
    }

    /**
     * Get the random hash for mail headers.
     * @return String A random hash for mail headers.
     */
    public function getHash() {
      return new String($this->hash);
    }

    /**
     * Get the MIME type of the file to attach.
     * @return String The MIME type of the file to attach.
     */
    public function getMimeType() {
      return new String($this->mimetype);
    }

  }

?>
